package com.bobochang.warehouse.controller;

import com.bobochang.warehouse.annotation.BusLog;
import com.bobochang.warehouse.constants.WarehouseConstants;
import com.bobochang.warehouse.entity.*;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.WorkRegionService;
import com.bobochang.warehouse.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LI
 * @date 2023/12/12
 */
@RequestMapping("/workregion")
@RestController
@BusLog(name = "工区管理")
public class WorkRegionController {
    @Autowired
    private WorkRegionService workRegionService;

    @Autowired
    private TokenUtils tokenUtils;
    /**
     * 查询所有工区
     * @return
     */
    @GetMapping("/workregion-list/{departmentId}")
    public Result unitList(@PathVariable Integer departmentId) {
        //执行业务
        List<WorkRegion> workRegions = workRegionService.queryAllWorkRegion(departmentId);
        //响应
        return Result.ok(workRegions);
    }


    @GetMapping("/workregion-info/{workregionId}")
    public Result getDepartmentIdInfo(@PathVariable Integer workregionId,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        return workRegionService.searchById(workregionId);
    }

    @GetMapping("/workregion-list")
    public Result userListPage(Page page, WorkRegion workRegion) {
        //执行业务
        page = workRegionService.queryWorkregionPage(page, workRegion);
        //响应
        return Result.ok(page);
    }

    @PostMapping("/addWorkregion")
    @BusLog(descrip = "添加工区")
    public Result addUser(@RequestBody WorkRegion workRegion,
                          @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即创建新用户的用户id
        //int createBy = currentUser.getUserId();
        //department.setCreateBy(createBy);
        //执行业务
        Result result = workRegionService.saveWorkregion(workRegion);
        return result;
    }

    @PutMapping("/updateWorkregion")
    @BusLog(descrip = "修改工区")
    public Result updateUserState(@RequestBody WorkRegion workRegion,
                                  @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即修改用户的用户id
        int updateBy = currentUser.getUserId();

        //department.setCreateBy(updateBy);

        //执行业务
        Result result = workRegionService.updateWorkregion(workRegion);

        //响应
        return result;
    }

    @DeleteMapping("/deleteWorkregion/{workregionId}")
    @BusLog(descrip = "删除部门")
    public Result deleteUser(@PathVariable Integer workregionId) {
        //执行业务
        workRegionService.deleteWorkregionById(workregionId);
        //响应
        return Result.ok("用户删除成功！");
    }




}
